<?php
include_once '../drivers/mysql.php';
include_once '../drivers/mongo.php';
//mysql链接
//$argv[1] = "root:root@192.168.128.128:3306/testdb";
//mongo链接
//$argv[3] = "firoyang:firoyang@192.168.128.128:27017/test";

//需要写入的collection
$collection = $argv[3];
//$collection = 'cc';
//需要构造多少条记录
$limit = $argv[4];
//额外字段的聚集间隔
$step = $argv[5];
if (preg_match('/(.+)\:(.+)\@(\d+\.\d+\.\d+\.\d+:\d+)\/(.+)/i', $argv[1], $mysqlInfo) == 0) {
	var_dump($mysqlInfo);
	exit('mysql参数错误,eg: root:root@192.168.128.128:3306/testdb');
};
if (preg_match('/(.+)\:(.+)\@(\d+\.\d+\.\d+\.\d+:\d+)\/(.+)/i', $argv[2], $mongoInfo) == 0) {
	exit('mongo参数错误,eg: firoyang:firoyang@192.168.128.128:27017/test');
};

$mongo = new myMongo($mongoInfo[3], $mongoInfo[1], $mongoInfo[2]);
$mongo->selectDB($mongoInfo[4]);
$mongo->selectCollection($collection);

$db = new mysql ( $mysqlInfo[3], $mysqlInfo[1], $mysqlInfo[2], $mysqlInfo[4] );
$resouce = $db->query("select * from Trouble;");
$count = mysql_num_rows($resouce);
$i=1;
while ($i++ <= $limit) {
	$row = $db->fetch($resouce);
	if (false === $row) {
		mysql_data_seek($resouce, 0);
		$i = $i - 1;
		continue;
	} else {
		$data = array();
		$data['id'] = $i;
		$data['title'] = $row['Title'];
		$data['area'] = $row['AffectArea'];
		$data['idlist'] = $row['AffectServiceGroupInfoIDList'];
		$data['memo'] = $row['Memo'];
		$data['time'] = $row['CreateTime'];
		$data['staffid'] = $row['CreateStaffID'];
		$data['staffname'] = $row['CreateStaffName'];
		$data['flag'] = $row['EnableFlag'];
		$data['sflag'] = $row['SearchSyncFlag'];
		$data['nid'] = round($i/$step);
		$mongo->insert($data);
	}
}

function rarray($array) {
	$count = count($array);
	$val = mt_rand(0, $count);
}
//随机日期
function rdate($format = 'Y-m-d H:i:s') {
    $min = strtotime('1990-01-01 00:00:00');
    $max = strtotime('2011-12-30 23:59:59');
    $val = mt_rand($min, $max);
    return date($format, $val);
}